﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;

namespace GW2LastOnline
{
    class Fonts
    {
        private static FontFamily cabin;

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);

        static Fonts()
        {
        }

        public static FontFamily GetFont(byte[] fontData)
        {
            PrivateFontCollection fonts = new PrivateFontCollection();

            IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length);
            Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
            fonts.AddMemoryFont(fontPtr, fontData.Length);

            uint pcf = 0;
            fonts.AddMemoryFont((IntPtr)fontPtr, fontData.Length);
            AddFontMemResourceEx((IntPtr)fontPtr, (uint)fontData.Length, IntPtr.Zero, ref pcf);

            Marshal.FreeCoTaskMem(fontPtr);

            FontFamily f = fonts.Families[0];
            fonts.Dispose();

            return f;
        }

        public static FontFamily Cabin
        {
            get
            {
                if (cabin == null)
                    cabin = GetFont(Properties.Resources.CabinRegular);

                return cabin;
            }
        }

        public static void Apply(FontFamily family, System.Windows.Forms.Control.ControlCollection controls)
        {
            foreach (System.Windows.Forms.Control control in controls)
            {
                control.Font = new Font(family, control.Font.Size, control.Font.Style);
            }
        }

        public static void Apply(FontFamily family, params System.Windows.Forms.Control[] controls)
        {
            foreach (System.Windows.Forms.Control control in controls)
            {
                control.Font = new Font(family, control.Font.Size, control.Font.Style);
            }
        }
    }
}
